package cn.itcast.aop;

import cn.itcast.exception.NameIsNullException;
import cn.itcast.exception.noAccountException;
import cn.itcast.exception.noMoneyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class ExceptionAdvice {
//这里只是处理了一小部分的异常,实际是会有各种各样的异常等着处理
    @ExceptionHandler(Exception.class)
    public ModelAndView handlerException(Exception e) {
        //定义一个异常信息
        ModelAndView modelAndView = new ModelAndView();
        //判断异常类型!
        if (e instanceof noMoneyException) {
            modelAndView.addObject("message", e.getMessage());
        } else if (e instanceof NameIsNullException) {
            modelAndView.addObject("message", e.getMessage());
        } else if(e instanceof noAccountException){
            modelAndView.addObject("message", e.getMessage());
        }else {
            modelAndView.addObject("message", "服务器内部错误,请联系管理员!!!");
        }
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
